﻿@model IEnumerable<Von.Web.Structs.IdentityUserInfo>
@{
    ViewData["Title"] = "系统登录人员信息管理";
}
<h2>系统登录人员信息管理<a class="btn btn-link" onclick="NewUser()">新建系统登录人员信息</a></h2>
<table id="userList" class="table">
    <thead>
        <tr>
            <th>登录名</th>
            <th>Email</th>
            <th>手机号</th>
            <th>锁定状态</th>
            <th>自动解锁时间</th>
            <th>显示姓名</th>
            <th>状态</th>
            <th>类型</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>@Html.DisplayFor(modelItem => item.LoginName)</td>
                <td>@Html.DisplayFor(modelItem => item.Email)</td>
                <td>@Html.DisplayFor(modelItem => item.PhoneNumber)</td>
                <td>@Html.DisplayFor(modelItem => item.LockoutEnabled)</td>
                <td>@Html.DisplayFor(modelItem => item.LockoutEnd)</td>
                <td>@Html.DisplayFor(modelItem => item.DisplayName)</td>
                <td>@Html.DisplayFor(modelItem => item.Status)</td>
                @switch (item.SysFlag)
                {
                    case Von.Web.Structs.IIdentity_Sys_Flag.IS_Host:
                        <td>平台管理员</td>
                        break;
                    case Von.Web.Structs.IIdentity_Sys_Flag.IS_Admin:
                        <td>系统管理员</td>
                        break;
                    case Von.Web.Structs.IIdentity_Sys_Flag.IS_Manage:
                        <td>业务管理员</td>
                        break;
                    case Von.Web.Structs.IIdentity_Sys_Flag.IS_User:
                        <td>注册用户</td>
                        break;
                    case Von.Web.Structs.IIdentity_Sys_Flag.IS_Guest:
                        <td>访客</td>
                        break;
                }
                <td>
                    <input class="btn-link" type="button" onclick="ChangePwd(this,'@item.ID')" value="重置密码" /> |
                    <input class="btn-link" type="button" onclick="Unlock(this,'@item.ID')" value="解锁" /> |
                    <input class="btn-link" type="button" onclick="EditUser(this,'@item.ID')" value="编辑" /> |
                    <input class="btn-link" type="button" onclick="ExtendDialog(this,'@item.ID')" value="辅助信息" /> |
                    @Html.ActionLink("拥有角色", "UserRoleManage", new { id = item.ID }, new { @class = "btn-link" })  |
                    <input class="btn-danger" type="button" onclick="DeleteUser(this,'@item.ID')" value="删除" />
                </td>
            </tr>
        }
    </tbody>
</table>
<!-- 模态框（Modal） -->
<div class="modal fade" id="userEditor" tabindex="-1" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true" data-mode="new">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header" style="background-color:aquamarine;">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="ModalLabel">系统登录人员信息编辑</h4>
            </div>
            <div class="modal-body">
                @Html.Partial("/Views/Account/RegisterView.cshtml", new Von.Web.Models.RegisterViewModel())
                <div class="form-group">
                    <label>人员类别</label>
                    <select name="SysFlag" id="SysFlag" class="form-control">
                        <option value="200">平台管理员</option>
                        <option value="100">系统管理员</option>
                        <option value="50">业务管理员</option>
                        <option value="1" checked>注册用户</option>
                        <option value="0">访客</option>
                    </select>
                    <span class="text-danger"></span>
                </div>
            </div>
            <div class="modal-footer">
                <div class="form-group">
                    <input id="ID" type="hidden" value="" />
                    <input value="保存" class="btn btn-default" onclick="SaveUser()" />
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>
</div>
<div class="modal fade" id="extendEditor" tabindex="-1" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true" data-mode="new">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header" style="background-color:aquamarine;">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="ModalLabel">系统登录人员辅助信息<a class="btn btn-link" onclick="NewExtend()">新建人员辅助信息</a></h4>
            </div>
            <div class="modal-body">
                <table id="extendList" class="table">
                    <thead>
                        <tr>
                            <th>项目名称</th>
                            <th>项目内容</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
            <div class="modal-footer">
                <div class="input-group">
                    <span class="input-group-addon">项目名称</span><input id="SubName" value="" class="form-control" />
                    <span class="input-group-addon">项目内容</span><input id="SubValue" value="" class="form-control" />
                    <span class="input-group-addon"><a class="btn-default" onclick="SaveExtend()">保存</a></span>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>
</div>
<div class="modal fade" id="pwdEditor" tabindex="-1" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header" style="background-color:aquamarine;">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="ModalLabel">重置密码</h4>
            </div>
            <div class="modal-body">
                <div class="input-group">
                    <span class="input-group-addon">重置密码</span><input id="PWD1" type="password" value="" class="form-control" />
                </div>
                <div class="input-group">
                    <span class="input-group-addon">验证密码</span><input id="PWD2" type="password" value="" class="form-control" />
                </div>
            </div>
            <div class="modal-footer">
                <a class="btn-default" onclick="ResetPwd(this)">重置</a>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>
</div>
<script type="text/javascript">
    function NewUser() {
        von.selectRow();
        SetRegisterData(null);
        von.editor.clear();
        von.editor.display("new");
    }
    function EditUser(btn, id) {
        von.selectRow(btn);
        AjaxPostData("@Url.Ajax("GetByIdentity")", { ID: id }, function (data) {
            von.editor.setData(data);
            SetRegisterData(data);
            von.editor.display("edit");
        });
    }
    function DeleteUser(btn, id) {
        if (confirm('确定删除吗？')) {
            von.selectRow(btn);
            AjaxPostData("@Url.Ajax("DeleteIdentity")", { 'ID': id }, function (result) { von.deleteRow(); });
        }
    }
    function Unlock(btn, id) {
        AjaxPostData("@Url.Ajax("UnlockIdentity")", { 'ID': id },
            function (result) { if (result = true) alert("Unlocked!"); else SysWarning(data); });
    }
    function ChangePwd(btn, id) {
        $("#ID").val(id);
        $("#pwdEditor").modal("show");
    }
    function ResetPwd(btn) {
        if ($('#PWD1').val() != $('#PWD2').val()) {
            alert("密码两次录入不一致！");
            return false;
        }
        regex = new RegExp("@ViewBag.PWDRegex.Replace("\\", "\\\\")", "g");
        if (!regex.test($('#PWD1').val())) {
            alert("@Html.Raw(ViewBag.PWDRequirement)！");
            return false;
        }
        AjaxPostData("@Url.Ajax("ResetPassword")", { UserID: $("#ID").val(), Password: $("#PWD1").val() }, function (data) { $("#pwdEditor").modal("hide"); });
    }
    function checkPwd() {
        if ($("#Password").val() != $("#PasswordConfirm").val()) {
            alert("密码两次录入不一致！");
            return false;
        } else return true;
    }
    function SaveUser() {
        var msg = CheckRegisterData();
        if (msg != "") return SysWarning(msg);
        var data = {};
        von.editor.getData(data);
        GetRegisterData(data);
        if (von.editor.mode == "new") AjaxPostData("/Account/RegistUser", data, function (data) { von.appendRow(von.getHtml(data)); von.editor.close(); });
        else AjaxPostData("@Url.Ajax("EditIdentity")", data, function (data) { von.replaceRow(von.getHtml(data)); von.editor.close(); });
    }
    @*========== ExtendDialog =========*@
    function getExtendRow(data) {
        return "<tr data-id='" + data.ID + "'><td>" + data.SubName + "</td><td>" + data.SubValue +
            "</td><td><input class=\"btn-link\" type=\"button\" onclick=\"EditExtend(this,'" + data.SubName + "')\" value=\"编辑\" /> | " +
            "<input class=\"btn-danger\" type=\"button\" onclick=\"DeleteExtend(this,'" + data.ID + "')\" value=\"删除\" /></td></tr>";
    }
    function ExtendDialog(btn, id) {
        $("#ID").val(id);
        AjaxPostData("@Url.Ajax("ListSubsidiary")", { ID: id }, function (result) {
            $("#extendList tbody tr").remove();
            for (data in result)
                $("#extendList tbody").append(getExtendRow(result[data]));
            $("#extendEditor").modal("show");
        });
    }
    function NewExtend(btn) {
        $("#SubName").val("");$("#SubValue").val("");
    }
    function EditExtend(btn) {
        var ps = $(btn).parent().siblings();
        $("#SubName").val(ps[0].innerText);
        $("#SubValue").val(ps[1].innerText);
    }
    function DeleteExtend(btn, id) {
        AjaxPostData("@Url.Ajax("DeleteSubsidiary")", { ID: id }, function (result) { $(btn).parents("tr").remove(); });
    }
    function SaveExtend(btn, userIdx) {
        AjaxPostData("@Url.Ajax("SaveSubsidiary")", { UserIdx: $("#ID").val(), Key: $("#SubName").val(), Val: $("#SubValue").val() }, function (result) {
            $("#extendList tbody tr[data-id='" + result.ID + "']").remove();
            $("#extendList tbody").append(getExtendRow(result));
        });
    }
</script>

@Html.Partial("~/Views/Shared/_TreeScriptsPartial.cshtml")
@Html.Import(EImportSection.BodyTrailler, "initUserTable",
    @<script>
         var von = VonGrid({
             app: "user",
             keyName: "ID",
             displayFields: ["LoginName", "Email", "PhoneNumber", "LockoutEnabled", "LockoutEnd", "DisplayName", "Status", "SysFlag"],
             fields: ["ID", "LoginName", "Email", "PhoneNumber", "LockoutEnabled", "LockoutEnd", "DisplayName", "Status", "SysFlag"],
             onAddBtns: function (data) { von.addLink(data.ID, "Unlock", "解锁"); von.addLink(data.ID, "EditUser", "编辑"); von.addLink(data.ID, "ExtendDialog", "辅助信息"); von.addAction("", "Admin", "UserRoleManage/" + data.ID, "拥有角色"); von.addDanger(data.ID, "DeleteUser", "删除"); }
         });
         var ext = VonGrid({
             app: "extend",
             keyName: "ID",
             displayFields: ["SubName", "SubValue"],
             fields: ["ID", "SubName", "SubValue"],
             onAddFields: function (data) {
             },
             onAddBtns: function (data) { this.addLink(data.ID, "ChangePwd", "重置密码"); this.addLink(data.ID, "EditExtend", "编辑"); this.addDanger(data.ID, "DeleteExtend", "删除"); }
         });
    </script>
)